package com.jlastu.dlxy_androidapp.view.main;

import android.content.Intent;
import android.util.Log;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.jlastu.dlxy_androidapp.R;
import com.jlastu.dlxy_androidapp.adapter.SchoolListAdapter;
import com.jlastu.dlxy_androidapp.base.BaseFragment;
import com.jlastu.dlxy_androidapp.contract.SchoolContract;
import com.jlastu.dlxy_androidapp.entity.School;
import com.jlastu.dlxy_androidapp.entity.SchoolPhoto;
import com.jlastu.dlxy_androidapp.presenter.SchoolPresenter;
import com.jlastu.dlxy_androidapp.view.details.CommodityDetails;
import com.jlastu.dlxy_androidapp.view.headers.SchoolHeader;

import java.util.ArrayList;
import java.util.List;

public class SchoolFragment extends BaseFragment implements View.OnClickListener, SchoolContract.ISchoolView {
    private RecyclerView mSchoolRecyclerView;
    private SchoolListAdapter mRecyclerAdapter;

    private SchoolContract.ISchoolPresenter mSchoolPresenter;

    private List<School> mSchoolList;

    private List<SchoolPhoto> mPhotoList;
    SchoolHeader mHeadView;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_school;
    }

    @Override
    protected void initView() {
        mSchoolPresenter = new SchoolPresenter(this, getContext());
        mSchoolList = new ArrayList<>();
        mPhotoList = new ArrayList<>();
        mSchoolRecyclerView = mView.findViewById(R.id.rv_school);
        StaggeredGridLayoutManager staggeredGridLayoutManager =
                new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL);
        mSchoolRecyclerView.setLayoutManager(staggeredGridLayoutManager);
        mRecyclerAdapter = new SchoolListAdapter(mSchoolList,mPhotoList);
        mSchoolRecyclerView.setAdapter(mRecyclerAdapter);
        mHeadView = mView.findViewById(R.id.school_headview);

    }

    @Override
    protected void initListener() {
        mHeadView.setLeftClickListener(this);
        mHeadView.setCenterClickListener(this);
        mHeadView.setRightClickListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        mSchoolPresenter.getSchoolList();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.button_left) {
            mHeadView.setLeftSelector();
            onResume();
        } else if (id == R.id.button_center) {
            mHeadView.setCenterSelector();
            OnClickCenter();

        } else if (id == R.id.button_right) {
            mHeadView.setRightSelector();
            OnClickRight();
        }
    }
    private void OnClickCenter(){
        String Classify = mHeadView.getCenterText();
        mSchoolPresenter.getSchoolList(Classify);
    }
    private void OnClickRight(){
        String Classify = mHeadView.getRightText();
        mSchoolPresenter.getSchoolList(Classify);
    }


    @Override
    public void showSchoolList(List<School> schoolList, List<SchoolPhoto> photoList) {
        mSchoolList = schoolList;
        mPhotoList = photoList;
        Log.d("", "showSchoolList " + mSchoolList.size());
        mRecyclerAdapter.updateDate(mSchoolList,mPhotoList);
    }
}
